/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/
package com.jinhe.tss.core.cachepool.container;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.log4j.Logger;

import com.jinhe.tss.core.cachepool.Cacheable;
import com.jinhe.tss.core.cachepool.CacheableKey;
import com.jinhe.tss.core.exception.BusinessException;

/** 
 * <p> EhcachePool.java </p> 
 * 
 * 使用本类来缓存的对象的key,value必须继承Serializable,这是因为ehcache这么要求.
 * 
 * @author Jon.King 2006-12-31
 */
public class EhcacheContainer extends AbstractPoolContainer {
    
    protected Logger log = Logger.getLogger(this.getClass());

    private Cache cache;
    
    public EhcacheContainer(String name){
        super(name);
        try {
            CacheManager.getInstance().addCache(name);
            cache = CacheManager.getInstance().getCache(name);
        } catch (IllegalStateException e) {
            log.error("EhcachePool->EhcacheContainer:IllegalStateException!", e);
        } catch (CacheException e) {
            log.error("EhcachePool->EhcacheContainer:CacheException!", e);
        }
    }
    
    public Cacheable get(Object key) {
        checkKey(key);
        try {
            Element e = cache.get((Serializable) key);
            if (e != null)
                return (Cacheable) e.getValue();
        } catch (IllegalStateException e) {
            log.error("EhcachePool->get:IllegalStateException!", e);
        } catch (CacheException e) {
            log.error("EhcachePool->get:CacheException!", e);
        }
        return null;
    }

    public Cacheable put(Object key, Cacheable value) {
        checkKey(key);
        checkKey(value);
        Element e = new Element((Serializable) key, (Serializable) value);
        cache.put(e);
        
        return value;
    }

    public Cacheable remove(Object key) {
        Cacheable item = get(key);
        checkKey(key);
        cache.remove((Serializable) key);
        return item;
    }

    @SuppressWarnings("unchecked")
    public List<CacheableKey> getKeys() {
        try {
            return cache.getKeys();
        } catch (IllegalStateException e) {
            log.error("EhcachePool->getKeys:IllegalStateException!", e);
        } catch (CacheException e) {
            log.error("EhcachePool->getKeys:CacheException!", e);
        }
        return null;
    }

    public void clear() {
        try {
            cache.removeAll();
        } catch (IllegalStateException e) {
            log.error("EhcachePool->clear:IllegalStateException!", e);
        } 
    }
    
    private void checkKey(Object key){
        if (!(key instanceof Serializable)) {
            log.error("类" + key.getClass() + "没有实现Serializable接口，不能被EHCache缓存！");
            throw new BusinessException("类" + key.getClass() + "没有实现Serializable接口，不能被EHCache缓存！");
        }
    }

    public int size() {
        try {
            return cache.getSize();
        } catch (Exception e) {
            log.error("获取EHCache缓存的大小时失败！", e);
            throw new BusinessException("获取EHCache缓存的大小时失败！", e);
        }    }

    public List<Cacheable> getValues() {
        List<CacheableKey> keys = this.getKeys();
        List<Cacheable> values = new ArrayList<Cacheable>();
        for(CacheableKey key : keys){
            Cacheable value = this.get(key);
            values.add(value);
        }
        return values;
    }
}

